package cn.com.fuhuitong.main.access.vm

import android.app.Application
import android.provider.ContactsContract
import androidx.lifecycle.MutableLiveData
import cn.com.fuhuitong.app.BaseApplication
import cn.com.fuhuitong.base.BaseViewModel
import cn.com.fuhuitong.http.BaseResponse
import cn.com.fuhuitong.http.HttpResponse
import cn.com.fuhuitong.http.ViewModelSubscribeListener
import cn.com.fuhuitong.main.access.entity.AccessResponse
import cn.com.fuhuitong.main.access.mode.AccessManager
import cn.com.fuhuitong.main.host.entity.ConfigResponse
import io.reactivex.disposables.Disposable

class AccessViewModel(application: Application) : BaseViewModel(application) {

    private val accessManager: AccessManager = AccessManager()

    val accessDataLiveData = MutableLiveData<HttpResponse<AccessResponse>>()

    val sendShortMessageDataLiveData = MutableLiveData<HttpResponse<BaseResponse>>()

    val resetpwdDataLiveData = MutableLiveData<HttpResponse<BaseResponse>>()

    /**
     * 登录
     */
    fun login(account: String, password: String) {
        val listener = object : ViewModelSubscribeListener<AccessResponse>(accessDataLiveData) {}
        val loginDisposable: Disposable = accessManager.login(account, password, listener)
        dispose.add(loginDisposable)
    }

    /**
     * 验证码登录
     */
    fun mobileLogin(mobile: String, captcha: String) {
        val listener = object : ViewModelSubscribeListener<AccessResponse>(accessDataLiveData) {}
        val mobileLoginDisposable: Disposable = accessManager.mobileLogin(mobile, captcha, listener)
        dispose.add(mobileLoginDisposable)
    }

    /**
     * 微信登录
     */
    fun weChatLogin(wxCode: String) {
        val listener = object : ViewModelSubscribeListener<AccessResponse>(accessDataLiveData) {}
        val weChatLoginDisposable: Disposable = accessManager.weChatLogin(wxCode, listener)
        dispose.add(weChatLoginDisposable)
    }

    /**
     * 验证码登录
     */
    fun mobileBind(
        inviteesMobile: String,
        inviteesName: String,
        nickname: String,
        identityCard: String,
        mobile: String,
        captcha: String
    ) {
        val listener = object : ViewModelSubscribeListener<AccessResponse>(accessDataLiveData) {}
        val mobileBindDisposable: Disposable =
            accessManager.mobileBind(
                BaseApplication.userToken(),
                inviteesMobile,
                inviteesName,
                nickname,
                identityCard,
                mobile,
                captcha,
                listener
            )
        dispose.add(mobileBindDisposable)
    }

    /**
     * 注册
     */
    fun register(
        mobile: String,
        nickname: String,
        password: String,
        code: String
    ) {
        val listener = ViewModelSubscribeListener<AccessResponse>(accessDataLiveData)
        val registerDisposable: Disposable =
            accessManager.register(
                mobile,
                nickname,
                password,
                code,
                listener
            )
        dispose.add(registerDisposable)
    }

    /**
     * 重置密码
     */
    fun resetpwd(mobile: String, newpassword: String, captcha: String) {
        val listener = ViewModelSubscribeListener<BaseResponse>(resetpwdDataLiveData)
        val resetpwdDisposable: Disposable =
            accessManager.resetpwd(mobile, newpassword, captcha, listener)
        dispose.add(resetpwdDisposable)
    }

    /**
     * 短信发送
     */
    fun sendShortMessage(mobile: String, event: String) {
        val listener = ViewModelSubscribeListener<BaseResponse>(sendShortMessageDataLiveData)
        val sendShortMessageDisposable: Disposable =
            accessManager.sendShortMessage(mobile, event, listener)
        dispose.add(sendShortMessageDisposable)
    }



    val configDataLiveData = MutableLiveData<HttpResponse<ConfigResponse>>()

    /**
     * 获取配置信息
     * */
    fun configValue(name: Array<String>) {
        val listener =
            object : ViewModelSubscribeListener<ConfigResponse>(configDataLiveData) {}
        val configValueDisposable: Disposable = accessManager.configValue(name,listener)
        dispose.add(configValueDisposable)
    }
}